package wap.rockvilletech.portal;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ChargeEtisalatWapUser
 */
public class LiveScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(LiveScoreServlet.class);
	
	@Override
	public void init() throws ServletException {
		super.init();
	}
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LiveScoreServlet() {
        super();
    }
    
	/**
	 * @see processRequest(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    	String livescore = "";    	
    	try {
   	    	livescore = DBProc.getInstance().getLiveScore();
    	    if("".equals(livescore))
    	    	livescore = "<div style=\"font-weight:bold;\">Live score is not available!</div>";
    	    
    	} catch(Exception e) {
    		log.error("", e);
    		livescore = "<div style=\"font-weight:bold;\">Live score is not available!</div>";
    	} finally {
    		printTextResponse(response, livescore);
    	}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	private void printTextResponse(HttpServletResponse resp, String text) {
		try {			
			PrintWriter out = resp.getWriter();
			resp.setContentType("text/plain");
			out.print(text);
			out.close();
		} catch (IOException e) {
			log.error("Error printing response: "+text, e);
		}		
	}	
}
